package com.ruoyi.web.controller.system;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysAppPrescriptionInfo;
import com.ruoyi.system.domain.SysAppPrescriptionInfoSort;
import com.ruoyi.system.domain.SysAppPrescriptionSort;
import com.ruoyi.system.service.ISysAppPrescriptionInfoService;
import com.ruoyi.system.service.ISysAppPrescriptionInfoSortService;
import com.ruoyi.system.service.ISysAppPrescriptionSortService;
import com.ruoyi.web.controller.form.InfoSortForm;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2020-10-23
 */
@Controller
@RequestMapping("/system/infosort")
public class SysAppPrescriptionInfoSortController extends BaseController {
	private String prefix = "system/infosort";

	@Autowired
	private ISysAppPrescriptionInfoSortService sysAppPrescriptionInfoSortService;

	@Autowired
	private ISysAppPrescriptionSortService sysAppPrescriptionSortService;
	
	@Autowired
	private ISysAppPrescriptionInfoService sysAppPrescriptionInfoService;

	@RequiresPermissions("system:sort:view")
	@GetMapping()
	public String sort() {
		return prefix + "/sort";
	}

	/**
	 * 查询【请填写功能名称】列表
	 */
	@RequiresPermissions("system:sort:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(SysAppPrescriptionInfoSort sysAppPrescriptionInfoSort) {
		startPage();
		List<SysAppPrescriptionInfoSort> list = sysAppPrescriptionInfoSortService
				.selectSysAppPrescriptionInfoSortList(sysAppPrescriptionInfoSort);
		return getDataTable(list);
	}

	/**
	 * 导出【请填写功能名称】列表
	 */
	@RequiresPermissions("system:sort:export")
	@Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(SysAppPrescriptionInfoSort sysAppPrescriptionInfoSort) {
		List<SysAppPrescriptionInfoSort> list = sysAppPrescriptionInfoSortService
				.selectSysAppPrescriptionInfoSortList(sysAppPrescriptionInfoSort);
		ExcelUtil<SysAppPrescriptionInfoSort> util = new ExcelUtil<SysAppPrescriptionInfoSort>(
				SysAppPrescriptionInfoSort.class);
		return util.exportExcel(list, "sort");
	}

	/**
	 * 新增【请填写功能名称】
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存【请填写功能名称】
	 */
	@RequiresPermissions("system:sort:add")
	@Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(SysAppPrescriptionInfoSort sysAppPrescriptionInfoSort) {
		return toAjax(sysAppPrescriptionInfoSortService.insertSysAppPrescriptionInfoSort(sysAppPrescriptionInfoSort));
	}

	/**
	 * 新增方剂信息
	 */
	@PostMapping("/saveSort")
	@ResponseBody
	public AjaxResult saveSort(InfoSortForm form) {
		SysAppPrescriptionInfoSort sysAppPrescriptionInfoSort = new SysAppPrescriptionInfoSort();
		sysAppPrescriptionInfoSort.setInfoId(form.getInfoId());
		sysAppPrescriptionInfoSort.setSortId(form.getSortId());
		sysAppPrescriptionInfoSort.setCreateTime(DateUtils.getNowDate());
		return toAjax(sysAppPrescriptionInfoSortService.insertSysAppPrescriptionInfoSort(sysAppPrescriptionInfoSort));
	}

	/**
	 * 修改【请填写功能名称】
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap) {
		SysAppPrescriptionInfoSort sysAppPrescriptionInfoSort = sysAppPrescriptionInfoSortService
				.selectSysAppPrescriptionInfoSortById(id);
		mmap.put("sysAppPrescriptionInfoSort", sysAppPrescriptionInfoSort);
		return prefix + "/edit";
	}

	/**
	 * 修改保存【请填写功能名称】
	 */
	@RequiresPermissions("system:sort:edit")
	@Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(SysAppPrescriptionInfoSort sysAppPrescriptionInfoSort) {
		return toAjax(sysAppPrescriptionInfoSortService.updateSysAppPrescriptionInfoSort(sysAppPrescriptionInfoSort));
	}

	/**
	 * 删除【请填写功能名称】
	 */
	@RequiresPermissions("system:sort:remove")
	@Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(sysAppPrescriptionInfoSortService.deleteSysAppPrescriptionInfoSortByIds(ids));
	}

	@PostMapping("/selectInfoBySortId")
	@ResponseBody
	public TableDataInfo selectInfoBySortId(InfoSortForm form) {
		Long infoId = form.getSortId();
		List<SysAppPrescriptionInfo> list = sysAppPrescriptionInfoService.selectInfoBySortId(infoId);
		return getDataTable(list);
	}

	/**
	 * 删除方剂和分类的关联关系
	 * 
	 * @Description:
	 * @Author 豫东一棵草
	 * @DateTime 2020年10月24日 下午10:46:19
	 * @param form
	 * @return
	 */
	@PostMapping("/deleteByInfoIdAndSortId")
	@ResponseBody
	public AjaxResult deleteByInfoIdAndSortId(InfoSortForm form) {
		SysAppPrescriptionInfoSort sysAppPrescriptionInfoSort = new SysAppPrescriptionInfoSort();
		sysAppPrescriptionInfoSort.setInfoId(form.getInfoId());
		sysAppPrescriptionInfoSort.setSortId(form.getSortId());
		return toAjax(sysAppPrescriptionInfoSortService.deleteByInfoIdAndSortId(sysAppPrescriptionInfoSort));
	}
	
	

}
